package device;

import communication.Communicator;
import communication.Dispatcher;
import communication.definitions.DeviceType;
import communication.definitions.Message;
import communication.definitions.TransmitableFunction;
import communication.exceptions.InvalidArgumentsException;

/**
 * The <code>Robot</code> is a type of <code>Device</code> which is responsible
 * for starting and stopping the robot.
 * 
 * @author Peter Kalauskas
 */
public class Robot extends Device {
	public Robot(byte id, Communicator comm) {
		super(DeviceType.ROBOT, id, comm);
	}

	/**
	 * Sends the start message to the robot, this will initialize the modules on
	 * the robot.
	 */
	public void start() {
		try {
			comm.send(new Message(this, TransmitableFunction.START_ROBOT,
					new byte[0]));
		} catch (InvalidArgumentsException e) {
			// an exception should ONLY occur if the START_ROBOT function is
			// tampered with
			e.printStackTrace();
		}
	}

	/**
	 * Sends the halt signal to the robot. This should shut down the robot.
	 */
	public void halt() {
		try {
			comm.send(new Message(this, TransmitableFunction.HALT_ROBOT,
					new byte[0]));
		} catch (InvalidArgumentsException e) {
			// an exception should ONLY occur if the HALT_ROBOT function is
			// tampered with
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws InterruptedException,
			InvalidArgumentsException {
		Dispatcher d = new Dispatcher();
		Communicator comm = new Communicator("COM3", d);

		Robot r = new Robot((byte) 0, comm);
		ADC adc = new ADC((byte) 0, comm);
		d.addDevice(r);
		d.addDevice(adc);

		r.start();

		while (true) {
			adc.requestValue();
			System.out.println(adc.getValue());
			Thread.sleep(250);
		}

		// Dispatcher d = new Dispatcher();
		// Communicator comm = new Communicator("COM3", d);
		//
		// Robot r = new Robot((byte) 0, comm);
		// Potentiometer pot = new Potentiometer((byte) 0, comm);
		// ServoController sc0 = new ServoController((byte) 0, comm, pot);
		// d.addDevice(r);
		// d.addDevice(pot);
		// d.addDevice(sc0);
		//
		// r.start();
		// pot.zero();
		// System.out.println(pot.getValue() + " set as zero for pot.");
		//
		// Thread.sleep(1000);
		//
		// sc0.start();
		// sc0.setServoDegree(45);
		// Thread.sleep(1000);
		// sc0.setServoDegree(180);
		//
		// while (true) {
		// pot.txValueRequest();
		// System.out.println(pot.getValue() + "  " + pot.getDegree());
		// Thread.sleep(250);
		// }
	}
}
